﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.IO;
using System.Text;

namespace PRI_SliverlightSS_TestWeb
{
    public partial class proxy : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string strReturn = string.Empty;
            try
            {
                string strURL = "";

                if (Request.QueryString.Count > 0)
                {
                    strURL = Request.QueryString[0];
                }
                else
                {
#if DEBUG
                    strURL = "http://www.google.com";
#else
                    Response.Write("<Error>Empty URL. [Usage: \"~/px.aspx?url=http://www.google.com)</Error>\"");
                    return;
#endif
                }

                WebRequest oWebRequest = WebRequest.Create(strURL);
                WebResponse oWebResponse = oWebRequest.GetResponse();
                Stream oReceiveStream = oWebResponse.GetResponseStream();
                StreamReader oStreamReader = new StreamReader(oReceiveStream);

                Response.ContentType = oWebResponse.ContentType;
                Response.Write(oStreamReader.ReadToEnd());

                oStreamReader.Close();
                oReceiveStream.Close();
                oWebResponse.Close();
            }
            catch (Exception ex)
            {
                Response.Clear();
                Response.Write(string.Format("<Error>{0}</Error>", ex.ToString()));
            }
        }
        static public string EncodeTo64(string _to_encode)
        {
            byte[] toEncodeAsBytes = System.Text.UTF8Encoding.UTF8.GetBytes(_to_encode);
            return System.Convert.ToBase64String(toEncodeAsBytes);
        }

        static public string DecodeFrom64(string _to_decode)
        {
            byte[] encodedDataAsBytes = System.Convert.FromBase64String(_to_decode);
            return System.Text.UTF8Encoding.UTF8.GetString(encodedDataAsBytes);
        }
    }
}
